package com.cnt.service.impl;


import com.cnt.dao.ReportDao;

import com.cnt.entity.Report;
import com.cnt.service.ReportService;
import com.cnt.util.FileUtil;
import com.cnt.vo.ReportDonationVo;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    //创建
    @Override
    public ResultVo<String> createReport(Report report) {
        if(report.getUserId()==0){
            return new ResultVo<>(1,"找不到该用户，添加失败",null);
        }
        if(report.getDonationId()==0){
            return new ResultVo<>(3,"找不到该募捐单，添加失败",null);
        }
        int insert = reportDao.insert(report);

        if(insert==1){
            return  new ResultVo<>(0,"添加成功",null);
        }
        return new ResultVo<>(2,"添加失败",null);
    }

    @Override
    public ResultVo<List<ReportDonationVo>> getReportDonationList(Integer minAge, Integer oldAge, String name) {
        List<ReportDonationVo> list= reportDao.getReportDonationList(minAge,oldAge,name);
        return  new ResultVo<>(0,"查询成功",list);
    }

    @Override
    public ResultVo<List<ReportDonationVo>> queryReportByUserId(Integer userId) {
        List<ReportDonationVo> list= reportDao.queryReportByUserId(userId);
        return  new ResultVo<>(0,"查询成功",list);
    }

    @Override
    public ResultVo<String> rejectReport(Integer reportId) {
        Report report = reportDao.selectById(reportId);
        if(report == null){
            return new ResultVo<>(1,"找不到该举报单",null);
        }
        report.setStatus("已驳回");
        int i = reportDao.updateById(report);
        if(i == 1){
            return new ResultVo<>(0,"已驳回该举报单",null);
        }
        return new ResultVo<>(2,"操作失败",null);
    }

    @Override
    public ResultVo<String> uploadFile(MultipartFile file, Integer reportId) {
        // 检查文件大小
        if (file.getSize() > 16 * 1024 * 1024) { // 文件大小超过16MB
            return new ResultVo<>(2, "文件大于16MB，文件上传失败", null);
        }
        Report report = reportDao.selectById(reportId);
        FileUtil fileUtil = new FileUtil();
        File file1 = null;
        try {

            file1 = fileUtil.convertMultipartFileToFile(file);
            try {
                File file2 = fileUtil.convertToPdf(file1);
                byte[] bytes = fileUtil.addWatermarkToPDF(file2, "捐赠平台专用");
                report.setFile(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
            file1.delete();
        } catch (IOException e) {
            file1.delete();
            e.printStackTrace();
        }
        if(reportDao.updateById(report)==1){
            return new ResultVo<>(0,"上传文件成功","");
        }
        return new ResultVo<>(1,"上传文件失败","");
    }

    @Override
    public InputStream downloadFile(Integer reportId) {
        Report report = reportDao.selectById(reportId);
        return new ByteArrayInputStream(report.getFile());
    }

    @Override
    public ResultVo<String> changeStateByAdmin(Integer reportId) {
        Report report = reportDao.selectById(reportId);
        if(report == null){
            return new ResultVo<>(1,"找不到该举报单",null);
        }
        if(Objects.equals(report.getStatus(), "未处理")){
            report.setStatus("待处理");
        }
        else if(Objects.equals(report.getStatus(), "待处理")){
            report.setStatus("未处理");

        }
        else{
            return new ResultVo<>(3,"你无权修改此举报的状态",null);
        }
        int i = reportDao.updateById(report);
        if(i == 1){
            return new ResultVo<>(0,"修改举报单状态成功",null);
        }
        return new ResultVo<>(2,"操作失败",null);
    }

    @Override
    public ResultVo<String> changeStateBySuperAdmin(Integer reportId) {
        Report report = reportDao.selectById(reportId);
        if(report == null){
            return new ResultVo<>(1,"找不到该举报单",null);
        }
        if(Objects.equals(report.getStatus(), "待处理")){
            report.setStatus("已处理");
        }
        else if(Objects.equals(report.getStatus(), "已处理")){
            report.setStatus("待处理");
        }
        else{
            return new ResultVo<>(3,"你无权修改此举报的状态",null);
        }
        int i = reportDao.updateById(report);
        if(i == 1){
            return new ResultVo<>(0,"修改举报单状态成功",null);
        }
        return new ResultVo<>(2,"操作失败",null);
    }
}
